program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
type
  pelement = ^element;
  element = record
    value: integer;
    value11: integer;
    value12: integer;
    value21: integer;
    value22: integer;
    value23: integer;
    value31: integer;
    value32: integer;
    next: pelement;
  end;
procedure AddToBegin1 (var head: pelement; num11, num12: integer);
var
  tmp: pelement;
begin
  tmp:=head;
  new(head);
  head^.value11:=num11;
  head^.value12:=num12;
  head^.next:=tmp;
end;





procedure AddToBegin2 (var head: pelement; num21, num22: integer);
var
  tmp: pelement;
begin
  tmp:=head;
  new(head);
  head^.value21:=num21;
  head^.value22:=num22;
  head^.next:=tmp;
end;




procedure AddToEnd1 (var head: pelement; num11, num12: integer);
var
  current: pelement;
begin
  if (head<>nil) then
  begin
    current:= head;
    while (current^.next<>nil) do
    begin
      current:= current^.next;
      new(current^.next);
      current^.next^.value11:= num11;
      current^.next^.value12:= num12;
      current^.next^.next:= nil;
    end;
    end else begin
      AddToBegin1(head, num11, num12);
  end;
end;





procedure AddToEnd2 (var head2: pelement; num21, num22: integer);
var
  current2: pelement;
begin
  if (head2<>nil) then
  begin
    current2:= head2;
    while (current2^.next<>nil) do
    begin
      current2:= current2^.next;
      new(current2^.next);
      current2^.next^.value21:= num21;
      current2^.next^.value22:= num22;
    end;
  end;
end;






procedure PrintList3 (head3: pelement);
var
  current3: pelement;
begin
  current3:=head3;
  while (current3<>nil) do
  begin
    if (current3<>nil) then
    begin
      writeln (current3^.value31, 'x', current3^.value32, ' ', '+', ' ');
    end else begin
      writeln (current3^.value31, 'x', current3^.value32);
    end;
    current3:= current3^.next;
  end;
end;







procedure PrintList2 (head4: pelement);
var
  current2: pelement;
begin
  current2:=head4;
  while (current2<>nil) do
  begin
    if (current2^.value23=0) then
    begin
      writeln ('+', ' ', current2^.value21, 'x', current2^.value22);
    end;
    current2:= current2^.next;
  end;
end;






var
  n, m, i, j, num11, num12, num21, num22, value23, value: integer;
  fin: textfile;
  head, current, current2, head2, head3, current3, head4: pelement;
begin
  value23:= 0;
  assignfile(fin, 'input.txt');
  reset(fin);
  readln(fin, n);
  for i := 1 to n do
  begin
    read(fin, num11);
    readln(fin, num12);
    AddToEnd1(head, num11, num12);
  end;
  readln(fin, m);
  for i := 1 to m do
  begin
    read(fin, num21);
    readln(fin, num22);
    AddToEnd2(head2, num21, num22);
  end;
  current:= head;
  current2:= head2;
  current3:= head3;
  for i:= 1 to n do
  begin
    for j := 1 to m do
    begin
      if (current^.value12=current2^.value22) then
      begin
        current^.value11:=current^.value11+current2.value21;
      end else begin
      current2:=current2^.next;
      current2^.value23:=1;
      end;
    end;
    current3^.value31:=current^.value11;
    current3^.value32:=current^.value12;
    new(current3^.next);
    current3:=current3^.next;
    current:= current^.next;
  end;
  PrintList3(current3);
  PrintList2(current2);
  readln;
end.
